package baidu.testProxy;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;

public class TestProxy {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Class<?> proxyClass = Proxy.getProxyClass(Monkey.class.getClassLoader(), Monkey.class.getInterfaces());
		MyInvocationHandler myInvocationHandler = new MyInvocationHandler(new Monkey());
		try {
//			Class[] constructorParams = {MyInvocationHandler.class};
//			Constructor<?> constructor = proxyClass.getConstructor(constructorParams);
//			Object proxy = constructor.newInstance(myInvocationHandler);
			
			Object proxy = Proxy.newProxyInstance(Monkey.class.getClassLoader(),
					Monkey.class.getInterfaces(),myInvocationHandler);
			Mammal mammal = (Mammal) proxy;
			mammal.eat("Banana");
			
			Primate primate = (Primate) proxy;
			primate.think();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
